package genetica.cromosomas.genes;

import java.util.List;

public class GenBool implements Gen {
	protected boolean genValue;

	public GenBool(boolean value) {
		this.genValue = value;
	}

	public boolean getGenValue() {
		return genValue;
	}

	public void setGenValue(boolean genValue) {
		this.genValue = genValue;
	}

	public static GenBool[] decTiBin(int entrada) {
		int tam = Double.valueOf(Math.ceil(Math.log(entrada) / Math.log(2)))
				.intValue();
		GenBool[] result = new GenBool[tam];
		for (int i = 0; i < tam; i++) {
			result[i] = new GenBool(((entrada % 2) == 1));
			entrada >>= 1;
		}
		return result;
	}
	public static Integer binToDec(List<Gen> gen) {
		if (gen == null)
			return null;

		Integer acumulado = 0;
		for (Gen b : gen) {
			GenBool genBool = null;
			if (!(gen instanceof GenBool))
				return null;
			genBool = (GenBool)b;
			acumulado <<= 1;
			acumulado += (genBool.getGenValue()) ? 1 : 0;
			
		}
		return acumulado;
	}
	@Override
	public Gen getAleatGen(){
		return new GenBool(r.nextBoolean());
	}
	
	@Override
	public boolean equals(Object o){
		if (o instanceof GenBool){
			return ((GenBool) o).genValue == this.genValue;
		}
		return false;
	}

	@Override
	public int compareTo(Gen o) {
		if (o instanceof GenBool)
			throw new Error("Genes distintos");
		if (this.genValue == ((GenBool)o).genValue)
			return 0;
		if (this.genValue)
			return 1;
		return -1;
	}

}
